package com.baizhi.service.impl;

import com.baizhi.entity.PmsAttrGroup;
import com.baizhi.dao.PmsAttrGroupMapper;
import com.baizhi.entity.PmsCategory;
import com.baizhi.service.IPmsAttrGroupService;
import com.baizhi.service.IPmsCategoryService;
import com.baizhi.vo.PmsAttrGroupVo;
import com.baizhi.vo.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 属性分组 服务实现类
 * </p>
 *
 * @author lby
 * @since 2020-12-09
 */
@Service
public class PmsAttrGroupServiceImpl extends ServiceImpl<PmsAttrGroupMapper, PmsAttrGroup> implements IPmsAttrGroupService {
    @Autowired
    private IPmsCategoryService categoryService;

    @Override
    public R selectAttrGroupBySizeAndCateId(Integer page, Integer limit, Long categoryId) {
        if (categoryId == null){
            return R.ok().put("data",null);
        }
//        分页查询当前分类下的所有属性分组信息
        Page<PmsAttrGroup> groupPage = this.page(
                new Page<PmsAttrGroup>(page, limit),
                new QueryWrapper<PmsAttrGroup>().eq("category_id", categoryId)
        );
        return R.ok().put("data",groupPage.getRecords()).put("count",groupPage.getTotal());
    }

    @Override
    public void addAttrGroup(PmsAttrGroupVo vo) {
//        1.根据分类的名字查询分类的id
        PmsCategory category = categoryService.getOne(
                new QueryWrapper<PmsCategory>()
                        .eq("cat_level", 3)
                        .eq("name", vo.getCategoryName())
        );
//        2.将所有数据从vo转到PmsAttrGroup对象中 添加数据库
        PmsAttrGroup attrGroup = new PmsAttrGroup();
        /**
         * 将a对象的数据 复制到b对象中
         * 按照属性的名字 要求属性名一样
         * 参数1 被复制对象
         * 参数2 目标对象
         */
        BeanUtils.copyProperties(vo,attrGroup);
        attrGroup.setCategoryId(category.getCatId());

        this.save(attrGroup);
    }
}
